using System.Text;

namespace Certs.Apis;

public class CertsClientProxy
{
    public IClient Client { get; protected set; }
    public HttpClient Http { get; protected set; }

    public CertsClientProxy(IHttpClientFactory httpClientFactory)
    {
        Http = httpClientFactory.CreateClient("CertsApi");
        Client = new Client(default, Http);
    }

    public async Task<byte[]> DownloadPem(Guid ticketId, CancellationToken cancellationToken)
    {
        var urlBuilder = new StringBuilder("api/ticket/");
        urlBuilder.Append(Uri.UnescapeDataString(ticketId.ToString()));

        var requestUri = new Uri(urlBuilder.ToString());

        var httpReq = new HttpRequestMessage(HttpMethod.Get, requestUri);

        var response = await Http.SendAsync(httpReq, cancellationToken);
        response.EnsureSuccessStatusCode();

        return await response.Content.ReadAsByteArrayAsync(cancellationToken);
    } 
}